package com.du.User.controller;

import com.du.User.pojo.Group;
import com.du.User.pojo.User;
import com.du.User.service.UserService;
import com.du.User.vo.UserQuery;
import com.du.common.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.expression.Ids;

import java.util.List;

@Controller
@RequestMapping("/stu")
public class StuController {

    @Autowired
    private UserService userService;


    @GetMapping("/list")
    @ResponseBody
    public Result<Object> getUserList(UserQuery param){
        List<User> list = userService.getUserList(param);
        Long count = userService.countUserList(param);
        return Result.success(list,count);
    }

    @GetMapping("")
    public String toStuListUI(){
        return "stu/stuList";
    }

    @PostMapping("")
    @ResponseBody
    public Result<Object> addUser(User user){
        userService.addUser(user);
        return Result.success("增加成功！");
    }


    @GetMapping("/add/ui")
    public String toAddUI(Model model){
        List<Group> groupList = userService.getAllGroup();
        model.addAttribute("groupList",groupList);
        return "stu/stuAdd";
    }


    @DeleteMapping("/{ids}")
    @ResponseBody
    public Result<Object> deleteUserByIds(@PathVariable("ids") String ids){
        userService.deleteUserByIds(ids);
        return Result.success("删除员工成功！");
    }

    @GetMapping("/{sid}")
    public String getUserById(@PathVariable("sid") Integer sid,Model model){
        User user = userService.getUserById(sid);
        model.addAttribute("user",user);
        return "stu/stuEdit";
    }

    @PutMapping("")
    @ResponseBody
    public Result<Object> updateUser(User user){
        userService.updateUser(user);
        return Result.success("修改成功！");
    }
}
